const router = require('koa-router')()
const ico = require("../ico");
router.prefix('/api/upload')

const { FilePath } = require('../../Utils/upload')


router.get('/Getupload', async (ctx, next) => {
  const file = ctx.request.files.file; // 获取上传的文件
  ctx.body = {
    token_data:ctx.token_data,
    files: await FilePath(file)
  }
})

router.post('/Postupload', async (ctx, next) => {
  const file = ctx.request.files.image; // 获取上传的文件
  const fileList = await FilePath(file);
  let addFilse = [];
  fileList.forEach(item => {
    addFilse.push({
      uuid:item['uuid'],
      filename:item['fileName'],
      filetype:item['ext'],
      filesize:item['size'],
      filepath:item['path'],
      fileOgname:item['originalFilename'],
      filemimetype:item['mimetype'],
    })
  });
  if(addFilse.length > 0){
    let res = await ico["upload"].bulkCreate(addFilse);
  }
  ctx.body = {
    token_data:ctx.token_data,
    files: fileList
  }
})

module.exports = router
